package com.xxgc.demo.controller;

import com.xxgc.demo.controller.result.Result;
import com.xxgc.demo.controller.vo.QuartzTaskVO;
import com.xxgc.demo.utils.SchedulerUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.quartz.Job;
import org.quartz.SchedulerException;
import org.springframework.web.bind.annotation.*;

@Slf4j
@Tag(name = "添加任务调度")
@RestController
@RequestMapping("/quartz")
public class UserQuartzController {

    @Operation(summary = "添加任务",description = "任务")
    @PostMapping("/addTask")
    public Result addTask(@RequestBody QuartzTaskVO qt) throws SchedulerException, ClassNotFoundException {

        SchedulerUtil.create()
                .withTrigger(qt.getTriggerName(), qt.getTriggerGroup(), qt.getCron())
                .withJob(qt.getJobName(), qt.getJobGroup(), (Class<? extends Job>) Class.forName(qt.getJobClass()))
                .schedule();

        return Result.ok("任务添加成功");
    }


}